package com.innovation.chengzhangriji.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.google.gson.JsonElement;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.DiscoverSaveComment;
import com.innovation.chengzhangriji.DiscoverSaveCommentDao;
import com.innovation.chengzhangriji.database.DatabaseManager;
import com.innovation.chengzhangriji.http.Client;
import com.innovation.chengzhangriji.http.ResponseHandler;
import com.innovation.chengzhangriji.preferences.LoginInfo;

import java.util.ArrayList;
import java.util.List;

public class SyncLocalCommentService extends Service {

    private Client.NetworkUnavailableListener networkUnavailableListener = new Client.NetworkUnavailableListener() {
        @Override
        public void networkUnavailable() {
            stopSelf();
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        final LoginInfo loginInfo = Config.getLoginInfo(this);
        if(loginInfo == null) {
            return super.onStartCommand(intent, flags, startId);
        }

        // 获取离线评论
        List<DiscoverSaveComment> comments = DatabaseManager.getInstance(this)
                .getLocalComments(loginInfo.getUserID());
        if(comments == null) {
            return super.onStartCommand(intent, flags, startId);
        }

        // 循环上传评论
        for (final DiscoverSaveComment comment : comments) {
            if(comment.getCommentType() == 1) {
                // 普通评论
                if(comment.getOperationState() == 1) {
                    // 新增一条评论
                    Client.requestComment(this, loginInfo.getPhone(), loginInfo.getToken().getAccessToken(), comment.getDiaryID(), comment.getIdentity(), comment.getContent(), comment.getReplyIdentity(), new ResponseHandler() {

                        @Override
                        public void onInnovationSuccess(JsonElement value) {
                            super.onInnovationSuccess(value);
                            // 标识该评论为在线评论
                            comment.setIsLocal(false);
                            DatabaseManager.getInstance(SyncLocalCommentService.this.getApplicationContext()).insertOrReplaceDiscoverSaveComment(comment);
                        }

                        @Override
                        public void onInnovationFailure(String msg, JsonElement value) {
                            super.onInnovationFailure(msg, value);
                            if(msg.equals("2034")) {
                                // 评论/点赞ID不存在时，评论失败，删除评论
                                DatabaseManager.getInstance(SyncLocalCommentService.this.getApplicationContext()).deleteComment(comment.getIdentity());
                            }
                        }
                    }, networkUnavailableListener);
                } else {
                    // 删除一条评论
                    Client.requestDeleteComment(this, loginInfo.getPhone(), loginInfo.getToken().getAccessToken(), comment.getIdentity(), new ResponseHandler() {

                        @Override
                        public void onInnovationSuccess(JsonElement value) {
                            super.onInnovationSuccess(value);
                            // 删除本条评论
                            DatabaseManager.getInstance(SyncLocalCommentService.this.getApplicationContext()).deleteComment(comment.getIdentity());
                        }

                        @Override
                        public void onInnovationFailure(String msg, JsonElement value) {
                            super.onInnovationFailure(msg, value);
                            if(msg.equals("2034")) {
                                // 评论/点赞ID不存在时，评论失败，删除评论
                                DatabaseManager.getInstance(SyncLocalCommentService.this.getApplicationContext()).deleteComment(comment.getIdentity());
                            }
                        }
                    }, networkUnavailableListener);
                }
            } else {
                // 赞
                if(comment.getOperationState() == 1) {
                    // 新增赞
                    Client.requestLike(this, loginInfo.getPhone(), loginInfo.getToken().getAccessToken(), comment.getDiaryID(), comment.getIdentity(), new ResponseHandler() {

                        @Override
                        public void onInnovationSuccess(JsonElement value) {
                            super.onInnovationSuccess(value);
                            // 标识该评论为在线评论
                            comment.setIsLocal(false);
                            DatabaseManager.getInstance(SyncLocalCommentService.this.getApplicationContext()).insertOrReplaceDiscoverSaveComment(comment);
                        }

                        @Override
                        public void onInnovationFailure(String msg, JsonElement value) {
                            super.onInnovationFailure(msg, value);
                            if (msg.equals("2034")) {
                                // 评论/点赞ID不存在时，评论失败，删除评论
                                DatabaseManager.getInstance(SyncLocalCommentService.this.getApplicationContext()).deleteComment(comment.getIdentity());
                            }
                        }
                    }, networkUnavailableListener);
                } else {
                    // 取消赞
                    Client.requestUnLike(this, loginInfo.getPhone(), loginInfo.getToken().getAccessToken(), comment.getDiaryID(), comment.getIdentity(), new ResponseHandler() {

                        @Override
                        public void onInnovationSuccess(JsonElement value) {
                            super.onInnovationSuccess(value);
                            // 标识该评论为在线评论
                            comment.setIsLocal(false);
                            DatabaseManager.getInstance(SyncLocalCommentService.this.getApplicationContext()).deleteComment(comment.getIdentity());
                        }

                        @Override
                        public void onInnovationFailure(String msg, JsonElement value) {
                            super.onInnovationFailure(msg, value);
                            if(msg.equals("2034")) {
                                // 评论/点赞ID不存在时，评论失败，删除评论
                                DatabaseManager.getInstance(SyncLocalCommentService.this.getApplicationContext()).deleteComment(comment.getIdentity());
                            }
                        }
                    }, networkUnavailableListener);
                }
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }
}
